package javalearning.MultiThreads.news.day01;

import java.util.Date;

public class ThreadDemo {
    static final Object lockA = new Object();
    static final Object lockB = new Object();

    public static void main(String[] args) throws InterruptedException {
        ThreadGroup threadGroup = new ThreadGroup("hahh");
        Students students = new Students();
        TA1 ta1 = new TA1(students);
        TA2 ta2 = new TA2(students);
        Thread t1 = new Thread(threadGroup, ta1);
        Thread t2 = new Thread(threadGroup, ta2);
        t1.start();
        t2.start();
        Thread.sleep(2000);
        System.out.println(threadGroup.activeCount());
        threadGroup.interrupt();
    }


    static class MyRunnable extends Thread {
        private int num = 100;

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "------" + i);
                Thread.yield();
            }
        }
    }

    static class DieLock extends Thread {
        public DieLock(boolean flag) {
            this.flag = flag;
        }

        boolean flag;

        @Override
        public void run() {
            if (flag) {
                synchronized (lockA) {
                    System.out.println("A lock");
                    synchronized (lockB) {
                        System.out.println("B lock");
                    }
                }
            } else {
                synchronized (lockB) {
                    System.out.println("B lock");
                    synchronized (lockA) {
                        System.out.println("A lock");
                    }
                }
            }
        }
    }

    static class DieLockB {

    }

    static class TicketSell implements Runnable {
        private static Integer tickets = 100;
        private final Object object = new Object();

        @Override
        public void run() {
            boolean x = true;
            while (x) {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    x = false;
                }
            }
            System.out.println("執行完畢");
        }

        private static synchronized void fun() {
            while (tickets > 0) {
                try {
                    Thread.sleep(10);
                    Thread.currentThread().yield();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "出售第  " + tickets-- + "  张票");
            }
        }
    }

    static class ThreadSon extends Thread {
        public ThreadSon(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "开始执行 " + new Date());
            try {
                synchronized (this) {

                    Thread.currentThread().wait();

                    System.out.println("开始执行");
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "线程结束 " + new Date());
        }
    }
}
